<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>json</title>
</head>
<body>
    <script>
        /*
        * Json:字符串数据格式
        *   key:value形式
        *   第一种:
        *       {"id":1,"username":"貂蝉","age":18}
        *   第二种
        *       [
        *           {"id":1,"username":"貂蝉","age":18}
        *           {"id":2,"username":"吕布","age":30}
        *       ]
        *   切记注意:
        *       注意json数据类型
        * */
        //定义天气对象
        let weather ={
            city:"北京",
            date:"2088-08-08",
            wendu:"10°-23°",
            shidu:"22%"
        }
        //stringfiy(对象) 将制定对象转换为json格式字符串
        //1.将天气对象转换为JSON格式的字符串
        let str = JSON.stringify(weather)
        document.write(str+"<br>")
        //parse(字符串) 将指定json格式字符串解析成对象
        //2.将JSON格式字符串解析成功JS对象
        let weather2 = JSON.parse(str);
        document.write("城市:"+weather2.city+"<br>")
        document.write("日期:"+weather2.date+"<br>")
        document.write("温度:"+weather2.wendu+"<br>")
        document.write("湿度:"+weather2.shidu+"<br>")
        //json格式的字符串数据
        let user = '{"id":1,"username":"貂蝉","age":18}'
        //将user字符串转换成json对象{"id":1,"username":"貂蝉","age":18}
        let users = JSON.parse(user);
        document.write("id:"+users.id+"<br>")
        document.write("姓名:"+users.username+"<br>")
        document.write("年龄:"+users.age+"<br>")
        let userList = '[{"id":1,"username":"貂蝉","age":18},{"id":2,"username":"吕布","age":30},{"id":3,"username":"董卓","age":60}]'
        let jsonList = JSON.parse(userList);
        //document.write(jsonList+"<br>")
        document.write("id:"+jsonList[0].id+"<br>")
        document.write("姓名:"+jsonList[0].username+"<br>")
        document.write("年龄:"+jsonList[0].age+"<br>")
        document.write("id:"+jsonList[1].id+"<br>")
        document.write("姓名:"+jsonList[1].username+"<br>")
        document.write("年龄:"+jsonList[1].age+"<br>")
        document.write("id:"+jsonList[2].id+"<br>")
        document.write("姓名:"+jsonList[2].username+"<br>")
        document.write("年龄:"+jsonList[2].age+"<br>")
    </script>
</body>
</html>